package com.mock.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.mock.dao.SupportingMaterialDao;
import com.mock.model.DataTable;
import com.mock.model.SupportingMaterial;
import com.mock.utils.HibernateUtil;

public class SupportingMaterialDaoImpl extends GenericDaoImpl<SupportingMaterial, Long> implements SupportingMaterialDao {

	@Override
	public List<SupportingMaterial> getAllSupportingMaterials() {
		return super.getAll(SupportingMaterial.class);
	}

	@Override
	public SupportingMaterial getById(long id) {
		return super.getById(SupportingMaterial.class, id);
	}

	@Override
	public List<SupportingMaterial> getAllSupportingMaterialsByOrganisationId(
			long id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		Criteria criteria = session.createCriteria(SupportingMaterial.class, "sm");
		criteria.add(Restrictions.eq("sm.organisationId", id));
		List<SupportingMaterial> supportingMaterials = criteria.list();
		return supportingMaterials;
	}

	@Override
	public List<SupportingMaterial> getSupportingMaterialByQuery(
			DataTable datatable, long id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(SupportingMaterial.class, "sm");
		criteria.createAlias("sm.organisation", "organisation");
		criteria.setFetchMode("sm.organisation", FetchMode.JOIN);
		criteria.add(Restrictions.eq("organisation.organisationId", id));
		criteria.createAlias("sm.supportingMaterialType", "smt");
		criteria.setFetchMode("sm.supportingMaterialType", FetchMode.JOIN);
		criteria.createAlias("sm.user", "user");
		criteria.setFetchMode("sm.user", FetchMode.JOIN);
		// Get Sort Column Name
		String columnName = "url";
		switch (datatable.iSortCol_0) {
		case 0:
			columnName = "url";
			break;
		case 1:
			columnName = "description";
			break;
		case 2:
			columnName = "smt.type";
			break;
		case 3:			
			columnName = "user.account";
			break;
		case 4:
			columnName = "addedDate";
			break;
		case 5:
			columnName = "active";
			break;
		default:
			break;
		}

		// Get Sort Direction
		if ("asc".equalsIgnoreCase(datatable.sSortDir_0)) {
			criteria.addOrder(Order.asc(columnName));
		} else {
			criteria.addOrder(Order.desc(columnName));
		}
		// Filter in-active
		if ("".equalsIgnoreCase(datatable.sSearch_5)) {

		} else if ("Yes".equalsIgnoreCase(datatable.sSearch_5)) {
			criteria.add(Restrictions.eq("active", true));
		}

		// Paging
		criteria.setFirstResult(datatable.iDisplayStart);
		criteria.setMaxResults(datatable.iDisplayLength);
		
		List<SupportingMaterial> supportingMaterials = criteria.list();
		session.getTransaction().commit();
		return supportingMaterials;
	}

	@Override
	public int getRowCount() {
		return super.getRowCount(SupportingMaterial.class);
	}

}
